﻿using Location.API.Model;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Location.API
{
    public class LocationsContext
    {
        private readonly IMongoDatabase _database = null;

        public LocationsContext(IOptions<LocationSettings> settings)
        {
            var client = new MongoClient(settings.Value.ConnectionString);
            if (client != null)
                _database = client.GetDatabase(settings.Value.Database);
        }
        public async Task StartTransactionAsync(Action<IClientSessionHandle> action)
        {
            var session = await _database.Client.StartSessionAsync();
            session.StartTransaction();
            action(session);
            await session.CommitTransactionAsync();
        }
        public IMongoCollection<UserLocation> UserLocation
        {
            get
            {
                return _database.GetCollection<UserLocation>("UserLocation");
            }
        }
        public IMongoCollection<Locations> Locations
        {
            get
            {
                return _database.GetCollection<Locations>("Locations");
            }
        }

        public IMongoCollection<ResidentialAreasLocation> ResidentialAreasLocation
        {
            get
            {
                return _database.GetCollection<ResidentialAreasLocation>("ResidentialAreasLocation");
            }
        }

        public IMongoCollection<ExpressStoreLocation> ExpressStoreLocation
        {
            get
            {
                return _database.GetCollection<ExpressStoreLocation>("ExpressStoreLocation");
            }
        }
    }
}
